using System;
using System.Windows.Forms;
//using ErkerTech.DeltaScope.App.Controls;
using ErkerTech.DeltaScope.App.Models;

namespace ErkerTech.DeltaScope.App.Views
{
    public class OpenFilesDialog : View.DropDownForm 
    {
        private TextBox txtLeftFile;
        private TextBox txtRightFile;
        private Label lblLeftFile;
        private Label lblRightFile;
        private Button btnLeftFilePrompt;
        private Button btnRightFilePrompt;
        private Button btnCompare;
        private Button btnCancel;
        private OpenFileDialog openFileDialog1;

        private string _sLeftFile;
        private string _sRightFile;

        public OpenFilesDialog()
        {
            // This call is required by the Windows Form Designer.
            InitializeComponent();
        }

        public OpenFilesDialog(bool enableAnimation):base(enableAnimation)
        {
            // This call is required by the Windows Form Designer.
            InitializeComponent();
        }

        #region Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.txtLeftFile = new System.Windows.Forms.TextBox();
            this.txtRightFile = new System.Windows.Forms.TextBox();
            this.lblLeftFile = new System.Windows.Forms.Label();
            this.lblRightFile = new System.Windows.Forms.Label();
            this.btnCompare = new System.Windows.Forms.Button();
            this.btnCancel = new System.Windows.Forms.Button();
            this.btnLeftFilePrompt = new System.Windows.Forms.Button();
            this.btnRightFilePrompt = new System.Windows.Forms.Button();
            this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
            this.SuspendLayout();
            // 
            // txtLeftFile
            // 
            this.txtLeftFile.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.txtLeftFile.Location = new System.Drawing.Point(88, 24);
            this.txtLeftFile.Name = "txtLeftFile";
            this.txtLeftFile.Size = new System.Drawing.Size(360, 20);
            this.txtLeftFile.TabIndex = 0;
            this.txtLeftFile.Text = "";
            this.txtLeftFile.WordWrap = false;
            this.txtLeftFile.TextChanged += new System.EventHandler(this.txtLeftFile_TextChanged);
            // 
            // txtRightFile
            // 
            this.txtRightFile.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.txtRightFile.Location = new System.Drawing.Point(88, 48);
            this.txtRightFile.Name = "txtRightFile";
            this.txtRightFile.Size = new System.Drawing.Size(360, 20);
            this.txtRightFile.TabIndex = 1;
            this.txtRightFile.Text = "";
            this.txtRightFile.WordWrap = false;
            this.txtRightFile.TextChanged += new System.EventHandler(this.txtRightFile_TextChanged);
            // 
            // lblLeftFile
            // 
            this.lblLeftFile.Location = new System.Drawing.Point(8, 26);
            this.lblLeftFile.Name = "lblLeftFile";
            this.lblLeftFile.Size = new System.Drawing.Size(48, 16);
            this.lblLeftFile.TabIndex = 2;
            this.lblLeftFile.Text = "Left File:";
            this.lblLeftFile.UseMnemonic = false;
            // 
            // lblRightFile
            // 
            this.lblRightFile.Location = new System.Drawing.Point(8, 50);
            this.lblRightFile.Name = "lblRightFile";
            this.lblRightFile.Size = new System.Drawing.Size(56, 16);
            this.lblRightFile.TabIndex = 3;
            this.lblRightFile.Text = "Right File:";
            this.lblRightFile.UseMnemonic = false;
            // 
            // btnCompare
            // 
            this.btnCompare.Location = new System.Drawing.Point(352, 80);
            this.btnCompare.Name = "btnCompare";
            this.btnCompare.Size = new System.Drawing.Size(96, 24);
            this.btnCompare.TabIndex = 4;
            this.btnCompare.Text = "&Compare";
            this.btnCompare.Click += new System.EventHandler(this.btnCompare_Click);
            // 
            // btnCancel
            // 
            this.btnCancel.Location = new System.Drawing.Point(264, 80);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(80, 24);
            this.btnCancel.TabIndex = 5;
            this.btnCancel.Text = "Cancel";
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            // 
            // btnLeftFilePrompt
            // 
            this.btnLeftFilePrompt.Location = new System.Drawing.Point(68, 24);
            this.btnLeftFilePrompt.Name = "btnLeftFilePrompt";
            this.btnLeftFilePrompt.Size = new System.Drawing.Size(20, 20);
            this.btnLeftFilePrompt.TabIndex = 6;
            this.btnLeftFilePrompt.Text = "...";
            this.btnLeftFilePrompt.Click += new System.EventHandler(this.btnLeftFilePrompt_Click);
            // 
            // btnRightFilePrompt
            // 
            this.btnRightFilePrompt.Location = new System.Drawing.Point(68, 48);
            this.btnRightFilePrompt.Name = "btnRightFilePrompt";
            this.btnRightFilePrompt.Size = new System.Drawing.Size(20, 20);
            this.btnRightFilePrompt.TabIndex = 7;
            this.btnRightFilePrompt.Text = "...";
            this.btnRightFilePrompt.Click += new System.EventHandler(this.btnRightFilePrompt_Click);
            // 
            // openFileDialog1
            // 
            this.openFileDialog1.Filter = "HTML files|*.html|Text Files|*.txt|All Files|*.*";
            // 
            // OpenFilesDialog
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(454, 110);
            this.Controls.Add(this.btnRightFilePrompt);
            this.Controls.Add(this.btnLeftFilePrompt);
            this.Controls.Add(this.btnCancel);
            this.Controls.Add(this.btnCompare);
            this.Controls.Add(this.lblRightFile);
            this.Controls.Add(this.lblLeftFile);
            this.Controls.Add(this.txtRightFile);
            this.Controls.Add(this.txtLeftFile);
            this.Name = "OpenFilesDialog";
            this.ResumeLayout(false);

        }
        #endregion

        private void btnLeftFilePrompt_Click(object sender, EventArgs e)
        {
            openFileDialog1.Title = "Select Left file...";
            if(openFileDialog1.ShowDialog(this) == DialogResult.OK)
                txtLeftFile.Text = openFileDialog1.FileName;
        }

        private void btnRightFilePrompt_Click(object sender, EventArgs e)
        {
            openFileDialog1.Title = "Select Right file...";
            if(openFileDialog1.ShowDialog(this) == DialogResult.OK)
                txtRightFile.Text = openFileDialog1.FileName;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void txtLeftFile_TextChanged(object sender, EventArgs e)
        {
            _sLeftFile = txtLeftFile.Text;
        }

        private void txtRightFile_TextChanged(object sender, EventArgs e)
        {
            _sRightFile = txtRightFile.Text;
        }

        private void btnCompare_Click(object sender, EventArgs e)
        {
            // Check for the existance of both files...
            var lbLeftExists = System.IO.File.Exists(_sLeftFile);
            var lbRightExists = System.IO.File.Exists(_sRightFile);
            /*
            if (lbLeftExists && lbRightExists)
            {
                var loOwner = (MainForm) Owner;
                var loNewPage = new DeltaScopePage
                                    {
                                        ToolTipText = (_sLeftFile.Trim() + " vs. " + _sRightFile.Trim()),
                                        Text =
                                            (System.IO.Path.GetFileName(_sLeftFile) + " vs. " +
                                             System.IO.Path.GetFileName(_sRightFile))
                                    };

                loOwner.tabDeltaViews.Appearance = TabAppearance.Normal;
                var loDeltaBlocks = DeltaScopeApp.Engine.Compare(_sLeftFile, _sRightFile);
                if (loDeltaBlocks != null) loNewPage.Bind(loDeltaBlocks);
                loOwner.statusBar1.Panels[0].Text = String.Format("Delta Time: {0}. Memory Usages: {1:n}", 
                    DeltaScopeApp.Engine.ElapsedTime, 
                    DeltaScopeApp.Engine.PeakMemoryUsage);
                loOwner.tabDeltaViews.TabPages.Add(loNewPage);
                loOwner.tabDeltaViews.SelectedTab = loNewPage;
            }
            else if (lbLeftExists)
                MessageBox.Show(this, "The Right file selected does not exist.", "File Open Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			
            else if (lbRightExists)
                MessageBox.Show(this, "The Left file selected does not exist.", "File Open Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			
            else
                MessageBox.Show(this, "Both of the files selected do not exist.", "File Open Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
*/
            // Close this screen...
            Close();
        }
    }
}